package com.zengdb.mybatis.util;

import java.io.IOException;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
 * MyBatis工具类，提供SqlSession对象的获取
 * @author zengdb
 *
 */
public class MyBatisUtil {
	
	private static SqlSessionFactory sf;
	/**
	 * 私有构造方法，不允许外部创建实例
	 */
	private MyBatisUtil() {

	}

	static {
		try {
			
			/*
			 //读取数据的连接信息
			Properties p = new Properties();
			p.load(Resources.getResourceAsReader("db.properties"));
			//创建SqlSessionFactory工厂对象 
			sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"),p);
			*/
			
			sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @return 返回SqlSessino对象
	 */
	public static SqlSession openSession() {
		return sf.openSession();
	}

}
